﻿using AutoMapper;
using JuCheap.Core.Infrastructure;
using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.IService;
using JuCheap.Core.IService.AppService;
using JuCheap.Core.Model;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JuCheap.Core.WebApi.Controllers
{
    /// <summary>
    /// 产品分类服务api
    /// </summary>
    [Route("api/[controller]")]
    public class CategoryController : BaseApiController
    {
        private readonly IProductCategoryService _productCategoryService;
        private readonly IProductCategoryExtService _productCategoryExtService;
        private readonly IMapper _mapper;

        /// <summary>
        /// ctor
        /// </summary>
        public CategoryController(IProductCategoryService productCategoryService,
            IProductCategoryExtService productCategoryExtService,
            IMapper mapper)
        {
            _productCategoryService = productCategoryService;
            _productCategoryExtService = productCategoryExtService;
            _mapper = mapper;
        }

        /// <summary>
        /// 创建产品分类信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("create")]
        public async Task<ActionResult<string>> Create([FromBody]CategoryCreateRequestDto req)
        {
            if (req.ParentId.IsBlank())
            {
                req.ParentId = "0";
            }
            var dto = _mapper.Map<CategoryCreateRequestDto, ProductCategoryDto>(req);
            var id = await _productCategoryService.InsertAsync(dto);
            return Ok(id);
        }

        /// <summary>
        /// 分页获取产品分类信息
        /// </summary>
        [HttpPost]
        [Route("search")]
        public async Task<ActionResult<PagedResult<ProductCategoryDto>>> Search([FromBody]ProductCategoryFilter filter)
        {
            var list = await _productCategoryExtService.GetTableTreeAsync(filter);
            var records = new PagedResult<ProductCategoryDto>()
            {
                PageIndex = filter.PageIndex,
                PageSize = filter.PageSize,
                Datas = list,
                Records = list.Count()
            };
            return Ok(records);
        }

        /// <summary>
        /// 获取产品分类信息
        /// </summary>
        [HttpGet]
        [Route("{id}")]
        public async Task<ActionResult<ProductCategoryDto>> Get([FromRoute]string id)
        {
            var categoryDto = await _productCategoryService.FindAsync(id);
            if(categoryDto != null)
            {
                categoryDto.ParentId = categoryDto.ParentId == "0" ? string.Empty : categoryDto.ParentId;
            }
            return Ok(categoryDto);
        }

        /// <summary>
        /// 修改产品分类信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("edit")]
        public async Task<ActionResult<bool>> Edit([FromBody]CategoryEditRequestDto req)
        {
            if (req.ParentId.IsBlank())
            {
                req.ParentId = "0";
            }
            var dto = _mapper.Map<CategoryEditRequestDto, ProductCategoryDto>(req);
            await _productCategoryService.UpdateAsync(dto);
            return Ok(true);
        }

        /// <summary>
        /// 批量删除产品分类信息
        /// </summary>
        /// <param name="ids">id集合</param>
        /// <returns></returns>
        [HttpPost]
        [Route("delete")]
        public async Task<ActionResult<bool>> Delete([FromBody]IList<string> ids)
        {
            await _productCategoryService.DeleteAsync(ids);
            return Ok(true);
        }

        /// <summary>
        /// 获取产品分类树状结构(下拉框树状结构)
        /// </summary>
        [HttpGet]
        [Route("tree")]
        public async Task<ActionResult<IEnumerable<SelectTreeResponseDto>>> GetSelectTree([FromQuery]bool loadFirstLevel)
        {
            var list = await _productCategoryExtService.GetTreeAsync(loadFirstLevel, string.Empty);
            var treeList = list
                .Select(x => new SelectTreeResponseDto
                {
                    Id = x.Id,
                    PId = x.ParentId,
                    Value = x.Id,
                    Label = x.CategoryName
                });
            return Ok(treeList);
        }

        /// <summary>
        /// 获取产品分类树状结构(Tree树状结构)
        /// </summary>
        [HttpGet]
        [Route("tree-data")]
        public async Task<ActionResult<IEnumerable<TreeResponseDto>>> GetTree([FromQuery]string parentId)
        {
            var list = await _productCategoryExtService.GetTreeAsync(parentId.IsBlank(), parentId);
            var treeList = list
                .Select(x => new TreeResponseDto
                {
                    Key = x.Id,
                    Title = x.CategoryName,
                    Value = x.Id
                });
            return Ok(treeList);
        }
    }
}
